package Easy;

public class Solution7 {
    // 7.整数反转
    public int reverse(int x) {
        if (x == 0)
            return 0;
        boolean fu = false;
        String str = new Integer(x).toString();
        if (str.length() == 1)
            return x;
        // 提取负号
        try {
            str = str.substring(str.indexOf('-'), str.length());
            fu = true;
        } catch (Exception e) {

        }
        String strpost = new String();
        while (true) {
            try {
                strpost = str.substring(0, str.lastIndexOf('0'));
            } catch (Exception e) {

            }

            if (strpost.length() + 1 != str.length())
                break;
            str = strpost;
        }
        char[] chars = str.toCharArray();
        for (int i = fu ? 1 : 0, j = str.length() - 1; i <= (str.length() - 1) / 2; i++, j--) {
            char tag = chars[j];
            chars[j] = chars[i];
            chars[i] = tag;
        }
        int result = ((Double) Double.valueOf(new String(chars)) > Math.pow(2, 31) - 1) || ((Double) Double.valueOf(new String(chars)) < Math.pow(-2, 31)) ? 0 : (int) Integer.valueOf(new String(chars));
        return result;
    }

    public static void main(String[] args) {
        Solution7 s = new Solution7();
        System.out.println(s.reverse(1463847412));
    }
}
